package com.sunrise.xdoc.dataloader;

import com.sunrise.xdoc.entity.config.Country;
import com.sunrise.xdoc.entity.config.Group;
import com.sunrise.xdoc.entity.config.InternalCode;
import com.sunrise.xdoc.entity.config.InternalCode.InternalCodeType;
import com.sunrise.xdoc.entity.config.Organization;
import com.sunrise.xdoc.entity.user.*;
import com.sunrise.xdoc.entity.user.UserProfile.Sex;
import com.sunrise.xdoc.helper.DaoHelper;
import com.sunrise.xdoc.service.IInternalCodeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * This dataloader will generate config data, some sample data for development
 * team. Another dataloader for customer will be created later.
 * 
 */
public class XDocDataLoader {
    private static ApplicationContext appContext;
    private static DaoHelper daoHelper;
    private static IInternalCodeService internaCodeService;

    public static void main(String[] args) {
        appContext = new ClassPathXmlApplicationContext("/serviceContext.xml");
        daoHelper = (DaoHelper) appContext.getBean("daoHelper");
        internaCodeService = (IInternalCodeService) appContext.getBean("internalCodeService");
        initCountries();
        initFolks();
        initReligions();
        initCities();
        initWorkingMethods();
        initEducationStatus();
        initCertification();
        initCertMethod();
        initCertLevel();
        initUniversities();
        initCertSubjects();
        initComputerLevel();
        initChucVu();

        initOrganization();
        initGroup();
        initContractTypes();
        initUsers();
        initLabourContract();
        initWorkingTransition();
    }

    private static void initChucVu() {
        InternalCode chucVu = new InternalCode();
        chucVu.setType(InternalCodeType.CHUC_VU_CTY);
        chucVu.setCode("CHUC_VU_CTY/ENGINEER");
        chucVu.setName("Kỹ sư");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(chucVu);

        InternalCode chucVu2 = new InternalCode();
        chucVu2.setType(InternalCodeType.CHUC_VU_CTY);
        chucVu2.setCode("CHUC_VU_CTY/SENIOR_MANAGER");
        chucVu2.setName("Quản lý cấp cao");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(chucVu2);
    }

    private static void initWorkingTransition() {
        WorkingTransition transition = new WorkingTransition();
        transition.setNhanVien(daoHelper.getDao(User.class).findByCode("userCode"));
        transition.setGroupOld(daoHelper.getDao(Group.class).findByCode("Oxygen"));
        transition.setChucVuOld(internaCodeService.listByType(InternalCodeType.CHUC_VU_CTY).get(0));
        transition.setChucVuNew(internaCodeService.listByType(InternalCodeType.CHUC_VU_CTY).get(1));
        transition.setGroupNew(daoHelper.getDao(Group.class).findByCode("XDocument"));
        daoHelper.getDao(WorkingTransition.class).saveOrUpdate(transition);
    }

    private static void initLabourContract() {
        LabourContract contract = new LabourContract();
        contract.setCode("21042013");
        contract.setType(daoHelper.getDao(ContractType.class).findByCode("VO_HAN"));
        contract.setNgayKyHopDong(new Date());
        contract.setUser(daoHelper.getDao(User.class).findByCode("userCode"));
        daoHelper.getDao(LabourContract.class).saveOrUpdate(contract);
    }

    private static void initContractTypes() {
        ContractType type = new ContractType();
        type.setCode("1N");
        type.setKyHan(12);
        type.setName("Hợp đồng 1 năm");
        type.setSoNgayBaoTruoc(30);
        daoHelper.getDao(ContractType.class).saveOrUpdate(type);

        ContractType type2 = new ContractType();
        type2.setCode("2N");
        type2.setKyHan(24);
        type2.setName("Hợp đồng 2 năm");
        type2.setSoNgayBaoTruoc(30);
        daoHelper.getDao(ContractType.class).saveOrUpdate(type2);

        ContractType type3 = new ContractType();
        type3.setCode("VO_HAN");
        type3.setKyHan(null);
        type3.setName("Hợp đồng vô thời hạn");
        type3.setSoNgayBaoTruoc(60);
        daoHelper.getDao(ContractType.class).saveOrUpdate(type3);
    }

    private static void initGroup() {
        Group group = new Group();
        group.setCode("Oxygen");
        group.setName("Nhóm Oxygen");
        group.setOrganization(daoHelper.getDao(Organization.class).findByCode("CN_HO_CHI_MINH"));
        daoHelper.getDao(Group.class).saveOrUpdate(group);

        Group group2 = new Group();
        group2.setCode("XDocument");
        group2.setName("Nhóm XDocument");
        group2.setOrganization(daoHelper.getDao(Organization.class).findByCode("CN_HO_CHI_MINH"));
        daoHelper.getDao(Group.class).saveOrUpdate(group2);
    }

    private static void initOrganization() {
        Organization org = new Organization();
        org.setCode("CN_HO_CHI_MINH");
        org.setName("Chi nhánh Hồ Chí Minh");
        org.setAddress("354/41 Phan Văn Trị, quận Bình Thạnh, Tp Hồ Chí Minh");
        org.setPhone("0909123456");
        daoHelper.getDao(Organization.class).saveOrUpdate(org);
    }

    private static void initComputerLevel() {
        InternalCode certSubject = new InternalCode();
        certSubject.setType(InternalCodeType.COMPUTER_LEVEL);
        certSubject.setCode("COMPUTER_LEVEL/TD_A");
        certSubject.setName("Trình độ A");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certSubject);

        InternalCode certSubject1 = new InternalCode();
        certSubject1.setType(InternalCodeType.COMPUTER_LEVEL);
        certSubject1.setCode("COMPUTER_LEVEL/TD_B");
        certSubject1.setName("Trình độ B");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certSubject1);
    }

    private static void initCertSubjects() {
        InternalCode certSubject = new InternalCode();
        certSubject.setType(InternalCodeType.CERT_SUBJECT);
        certSubject.setCode("CERT_SUBJECT/CNPM");
        certSubject.setName("Công nghệ phần mềm");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certSubject);

        InternalCode certSubject1 = new InternalCode();
        certSubject1.setType(InternalCodeType.CERT_SUBJECT);
        certSubject1.setCode("CERT_SUBJECT/HTTT");
        certSubject1.setName("Hệ thống thông tin");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certSubject1);
    }

    private static void initCertLevel() {
        InternalCode certLevel1 = new InternalCode();
        certLevel1.setType(InternalCodeType.CERT_LEVEL);
        certLevel1.setCode("CERT_LEVEL/GIOI");
        certLevel1.setName("Giỏi");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certLevel1);

        InternalCode certLevel2 = new InternalCode();
        certLevel2.setType(InternalCodeType.CERT_LEVEL);
        certLevel2.setCode("CERT_LEVEL/KHA");
        certLevel2.setName("Khá");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certLevel2);
    }

    private static void initCertMethod() {
        InternalCode certMethod1 = new InternalCode();
        certMethod1.setType(InternalCodeType.CERT_METHOD);
        certMethod1.setCode("CERT_METHOD/CHINH_QUY");
        certMethod1.setName("Chính quy");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certMethod1);

        InternalCode certMethod2 = new InternalCode();
        certMethod2.setType(InternalCodeType.CERT_METHOD);
        certMethod2.setCode("CERT_METHOD/TAI_CHUC");
        certMethod2.setName("Tại chức");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certMethod2);
    }

    private static void initCertification() {
        InternalCode certification = new InternalCode();
        certification.setType(InternalCodeType.CERTIFICATION);
        certification.setCode("CERTIFICATION/CU_NHAN");
        certification.setName("Cử Nhân");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(certification);
    }

    private static void initEducationStatus() {
        InternalCode educationStatus = new InternalCode();
        educationStatus.setType(InternalCodeType.EDU_STATUS);
        educationStatus.setCode("EDU_STATUS/DH");
        educationStatus.setName("Đại học");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(educationStatus);
    }

    private static void initWorkingMethods() {
        InternalCode wm1 = new InternalCode();
        wm1.setType(InternalCodeType.WORKING_METHOD);
        wm1.setCode("WORKING_METHOD/FULL_TIME");
        wm1.setName("Full time");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(wm1);

        InternalCode wm2 = new InternalCode();
        wm2.setType(InternalCodeType.WORKING_METHOD);
        wm2.setCode("WORKING_METHOD/PART_TIME");
        wm2.setName("Part time");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(wm2);
    }

    private static void initCities() {
        InternalCode city1 = new InternalCode();
        city1.setType(InternalCodeType.CITY);
        city1.setCode("CITY/HO_CHI_MINH");
        city1.setName("TP Hồ Chí Minh");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(city1);

        InternalCode city2 = new InternalCode();
        city2.setType(InternalCodeType.CITY);
        city2.setCode("CITY/HA_NOI");
        city2.setName("TP Hà Nội");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(city2);
    }

    private static void initReligions() {
        InternalCode university1 = new InternalCode();
        university1.setType(InternalCodeType.RELIGION);
        university1.setCode("RELIGION/THIEN_CHUA");
        university1.setName("Thiên Chúa");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(university1);
    }

    private static void initFolks() {
        InternalCode university1 = new InternalCode();
        university1.setType(InternalCodeType.FOLK);
        university1.setCode("FOLK/KINH");
        university1.setName("Kinh");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(university1);
    }

    private static void initUniversities() {
        InternalCode university1 = new InternalCode();
        university1.setType(InternalCodeType.UNIVERSITY);
        university1.setCode("UNIVERSITY/KHTN-HCM");
        university1.setName("Trường Đại Học Khoa học Tự Nhiên thành phố Hồ Chí Minh");
        daoHelper.getDao(InternalCode.class).saveOrUpdate(university1);
    }

    private static void initCountries() {
        Country country = new Country();
        country.setCode("VI");
        country.setName("Viet Nam");
        daoHelper.getDao(Country.class).saveOrUpdate(country);
    }

    private static void initUsers() {
        User u = new User();
        u.setUsername("php");
        u.setPassword("123456");
        u.setCode("userCode");

        Role r = new Role(Role.ADMIN);
        r.setCode("roleCode");
        Set<Role> roles = new HashSet<>();
        roles.add(r);

        u.setRoles(roles);
        daoHelper.getDao(User.class).saveOrUpdate(u);

        UserProfile profile = new UserProfile();
        profile.setFirstName("Nguyen");
        profile.setMiddleNam("Van");
        profile.setLastName("B");
        profile.setSex(Sex.MAN);
        profile.setUniverstity(daoHelper.getDao(InternalCode.class).findByCode("UNIVERSITY/KHTN-HCM"));
        daoHelper.getDao(UserProfile.class).saveOrUpdate(profile);
    }

}
